package com.affablebeans.model.dal;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.affablebeans.model.entity.Category;

/**
 *
 * @author Bora SAYINER
 * @since Jan 25, 2015 6:37:43 PM
 * @version 1.0
 */
public class CategoryDAL extends AbstractDAL<Category> {

	private static final String INSERT_SQL = "INSERT INTO category (name) VALUES (?)";
	private static final String UPDATE_SQL = "UPDATE category SET name=? WHERE id=?";
	private static final String DELETE_SQL = "DELETE FROM category WHERE id=?";
	private static final String FIND_BY_ID_SQL = "SELECT * FROM category WHERE id=?";
	private static final String GET_ALL_SQL = "SELECT * FROM category";

	public CategoryDAL(DataSource dataSource) {
		super(dataSource, "category");
	}

	@Override
	public boolean insert(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createConnection()) {
			try (PreparedStatement statement = connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS)) {
				statement.setString(1, entity.getName());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
				if (result) {
					ResultSet generatedKeys = statement.getGeneratedKeys();
					if (generatedKeys.next()) {
						entity.setId(generatedKeys.getInt(1));
					}
				}
			}
		}
		return result;
	}

	@Override
	public boolean update(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createConnection()) {
			try (PreparedStatement statement = connection.prepareStatement(UPDATE_SQL)) {
				statement.setString(1, entity.getName());
				statement.setInt(2, entity.getId());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
			}
		}
		return result;
	}

	@Override
	public boolean delete(Category entity) throws SQLException {
		boolean result = false;
		try (Connection connection = createConnection()) {
			try (PreparedStatement statement = connection.prepareStatement(DELETE_SQL)) {
				statement.setInt(1, entity.getId());
				int affectedRows = statement.executeUpdate();
				result = affectedRows > 0;
			}
		}
		return result;
	}

	@Override
	public Category findById(int... id) throws SQLException {
		Category category = null;
		try (Connection connection = createConnection()) {
			try (PreparedStatement statement = connection.prepareStatement(FIND_BY_ID_SQL)) {
				statement.setInt(1, id[0]);
				ResultSet resultSet = statement.executeQuery();
				if (resultSet.next()) {
					category = createEntity(resultSet);
				}
			}
		}
		return category;
	}

	@Override
	public List<Category> getAll() throws SQLException {
		List<Category> categories = new ArrayList<Category>();
		try (Connection connection = createConnection()) {
			try (Statement statement = connection.createStatement()) {
				ResultSet resultSet = statement.executeQuery(GET_ALL_SQL);
				while (resultSet.next()) {
					categories.add(createEntity(resultSet));
				}
			}
		}
		return categories;
	}

	@Override
	Category createEntity(ResultSet resultSet) throws SQLException {
		Category category = new Category();
		category.setId(resultSet.getInt("id"));
		category.setName(resultSet.getString("name"));
		return category;
	}

}
